//
//  DescViewController.m
//  Account
//
//  Created by wanmeizty on 2016/11/29.
//  Copyright © 2016年 wanmeizty. All rights reserved.
//

#import "DescViewController.h"

#import "AccountTableViewCell.h"

@interface DescViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, strong) NSMutableArray * dataSource;


@end

@implementation DescViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"账单记录";
    self.view.backgroundColor = [UIColor whiteColor];
    
   self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"清空数据" style:UIBarButtonItemStylePlain target:self action:@selector(deleteAllNodes)];
    self.dataSource = [NSMutableArray array];
    [self createTableView];
    [self getDatas];
    // Do any additional setup after loading the view.
}

- (void)deleteAllNodes{
   
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本对话框" message:@"登录和密码对话框示例" preferredStyle:UIAlertControllerStyleAlert];

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"用户名";

    }];
    
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"密码";
        textField.secureTextEntry = YES;
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSArray * textfields = alertController.textFields;
        UITextField * namefield = textfields[0];
        UITextField * passwordfiled = textfields[1];
        
        if ([namefield.text isEqualToString:@"admin"] && [passwordfiled.text isEqualToString:@"zty123456"]) {
            [[FMDBManager shareManager] deleteTable];
            
            [self getDatas];
        }
        
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:sureAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
}

- (void)createTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
    [self.rootScrollView addSubview:self.tableView];
    
}

#pragma mark delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100 * [XMScreenAdaptation adapterMultipleByWidth] ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AccountTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        
        cell = [[AccountTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
    }
    
    AccountModel * model = [self.dataSource objectAtIndex:indexPath.row];
    NSLog(@"%@",model.name);
    [cell configTabelwithAccountModel:model];
    
    return cell;
    
}


- (void)getDatas{
    
    
    [self.dataSource removeAllObjects];
    
    [self.dataSource addObjectsFromArray:[[FMDBManager shareManager] query]];
    
    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
